package com.moneyhelper.common.rest;

import com.moneyhelper.common.biz.CommonBiz;
import com.moneyhelper.common.resp.RestResponse;
import com.moneyhelper.common.resp.TableResponse;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author： ygl
 * @date： 2019/06/30
 * @Description：
 */
public class CommonRest<Biz extends CommonBiz,Entity,PK> {

    @Autowired
    protected Biz biz;

    @PostMapping("")
    @ResponseBody
    @ApiOperation("新增单个对象")
    public RestResponse add(@RequestBody Entity entity){
        biz.insertSelective(entity);
        return RestResponse.OK(entity);
    }

    @GetMapping("/{id}")
    @ResponseBody
    @ApiOperation("查询单个对象")
    public RestResponse get(@PathVariable PK id){
        Object obj = biz.selectById(id);
        return RestResponse.OK(obj);
    }

    @PutMapping
    @ResponseBody
    @ApiOperation("更新单个对象")
    public RestResponse update(@RequestBody Entity entity){
        biz.updateSelectiveById(entity);
        return RestResponse.OK(entity);
    }

    @DeleteMapping("/{id}")
    @ResponseBody
    @ApiOperation("移除单个对象")
    public RestResponse remove(@PathVariable PK id){
        biz.deleteById(id);
        return RestResponse.OK();
    }

    @DeleteMapping
    @ResponseBody
    @ApiOperation("移除多个对象")
    public RestResponse remove(@RequestParam("ids") List<PK> ids){
        biz.deleteByIds(ids);
        return RestResponse.OK();
    }

    @GetMapping("/all")
    @ResponseBody
    @ApiOperation("获取所有数据")
    public RestResponse all()   {
        List<Entity> list = biz.selectAll();
        return RestResponse.OK(list);
    }

    @ApiOperation("分页获取数据")
    @RequestMapping(value = "/page",method = RequestMethod.GET)
    @ResponseBody
    public TableResponse<Entity> list(@RequestParam Map<String, Object> params){
        //查询列表数据
        return biz.selectByQuery(params);
    }

}
